﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.IO;
using System.Xml;

namespace Burn4You
{
    class DiskTemplateReader : DiskItem
    {
        DiskItem Current;

        XmlTextReader reader;

        public DiskTemplateReader()
        {

        }

        public List<DiskTemplate> GetTemplates()
        {
            List<DiskTemplate> templates = new List<DiskTemplate>();

            if (Directory.Exists(Burn4YouPlugin.settings.TemplateFolder))
            {
                DirectoryInfo di = new DirectoryInfo(Burn4YouPlugin.settings.TemplateFolder);
                FileInfo[] files = di.GetFiles("*.template");

                foreach (FileInfo file in files)
                {
                    DiskTemplate tmp = ReadTemplate(file.FullName);

                    if (tmp != null)
                        templates.Add(tmp);
                }
            }
            return templates;

        }

        void AddItem(DiskItem item)
        {
            if(Current != null)
                item.Parent = Current;

            if(Current != null)
                Current.Children.Add(item);

            Current = item;
        }

        void ReadItemGeneral(DiskItem item)
        {
            if (reader.Name == "ID")
            {
                int i;
                if (int.TryParse(reader.Value, out i))
                    item.ID = i;
            }
        }

        DiskTemplate ReadTemplate(String file)
        {

            DiskTemplate template = new DiskTemplate();

            DVDStructure disk = new DVDStructure();

            reader = new XmlTextReader(file);

            AddItem(disk);

            while (reader.Read())
            {
                switch(reader.NodeType)
                {
                    case XmlNodeType.Element:

                        reader.MoveToElement();

                        if (reader.Name.ToLower() == "template")
                        {
                            while (reader.MoveToNextAttribute())
                            {
                                if (reader.Name.ToLower() == "name")
                                    template.Name = reader.Value;

                                if (reader.Name.ToLower() == "previewimage")
                                {
                                    Utils.Burn4YouLog.Write("Disk Template Reader: Template PreviewImage values is: " + reader.Value);

                                    if (reader.Value.Contains('\\'))
                                        template.PreviewImage = reader.Value;
                                    else
                                        template.PreviewImage = Burn4YouPlugin.settings.TemplateFolder + "\\" + reader.Value;

                                    Utils.Burn4YouLog.Write("Disk Template Reader: Resulting PreviewImage path is: " + template.PreviewImage);
                                }

                                if (reader.Name.ToLower() == "description")
                                    template.Description = reader.Value;
                            }

                        }

                        if (reader.Name.ToLower() == "vmgm")
                        {
                            DVDvmgm VMGM = new DVDvmgm();

                            while(reader.MoveToNextAttribute())
                            {
                                ReadItemGeneral(VMGM);

                                if(reader.Name.ToLower() == "language")
                                    VMGM.Language = reader.Value;
                            }

                            AddItem(VMGM);
                        }

                        if (reader.Name.ToLower() == "titleset")
                        {
                            DVDTitleSet titleset = new DVDTitleSet();


                            while (reader.MoveToNextAttribute())
                            {
                                ReadItemGeneral(titleset);
                            }

                            AddItem(titleset);
                        }

                        if (reader.Name.ToLower() == "menu")
                        {
                            DVDMenu menu = new DVDMenu();


                            while (reader.MoveToNextAttribute())
                            {
                                ReadItemGeneral(menu);
                            }

                            AddItem(menu);
                        }

                        if (reader.Name.ToLower() == "title")
                        {
                            DVDTitle title = new DVDTitle();


                            while (reader.MoveToNextAttribute())
                            {
                                ReadItemGeneral(title);
                            }

                            AddItem(title);
                        }

                        if (reader.Name.ToLower() == "video")
                        {
                            DVDVideo video = new DVDVideo();

                            while (reader.MoveToNextAttribute())
                            {
                                ReadItemGeneral(video);


                                if (reader.Name.ToLower() == "pause")
                                {
                                    int result;
                                    if (int.TryParse(reader.Value, out result))
                                        video.Pause = result;
                                }
                                    
                            }

                            AddItem(video);
                        }

                        if (reader.Name.ToLower() == "audio")
                        {
                            DVDAudio audio = new DVDAudio();

                            AddItem(audio);
                        }

                        if (reader.Name.ToLower() == "subtitle")
                        {
                            DVDSubtitle subtitle = new DVDSubtitle();

                            AddItem(subtitle);
                        }

                        break;

                    case XmlNodeType.EndElement:
                        if(Current.Parent != null)
                            Current = Current.Parent;
                        break;

                    case XmlNodeType.Text:

                        break;
                }
            }

            template.Disk = disk;

            return template;
        }
    }
}
